package priv.conceit.study.book.lambda.chapter4;

import lombok.AllArgsConstructor;
import lombok.Data;
import priv.conceit.study.book.lambda.common.entity.Album;
import priv.conceit.study.book.lambda.common.entity.Artist;
import priv.conceit.study.book.lambda.common.entity.Track;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;

/**
 * 基本类型
 * 4.2
 *
 *
 * 拆箱和装箱会带来额外的性能损耗
 * Stream的某些方法对基本类型和包装类型做了区分
 *
 * 如果方法返回类型为基本类型，则在基本类型前加To
 * 比如ToLongFunction
 *
 * 反之则添加类型名即可
 * LongFunction
 *
 * 如果高阶函数使用基本类型，则在操作后添加To再加基本类型
 * 比如mapToLong
 *
 * 基本类型都有对应的Stream 以基本类型为前缀
 * 比如LongStream
 *
 * @author Conceit
 * @since 1.0.0, 2021/9/12
 */
@Data
@AllArgsConstructor
public class Primitives {

    /**
     * 统计曲目长度
     *
     * @param args
     */
    public static void main(String[] args) {
        List<Album> albums=new ArrayList<>();
        Album album1=new Album("专辑1",
                Arrays.asList(new Track("专辑1-歌曲1",100),new Track("专辑1-歌曲2",200)),
                Arrays.asList(new Artist("zhaohang","china"),new Artist("zhaojie","china")));
        Album album2=new Album("专辑2",
                Arrays.asList(new Track("专辑2-歌曲1",300),new Track("专辑2-歌曲2",500)),
                Arrays.asList(new Artist("zhaohang","china"),new Artist("zhaojie","china")));
        albums.add(album1);
        albums.add(album2);


        //高阶函数mapToInt 接受函数ToIntFunction
        IntSummaryStatistics intSummaryStatistics=albums.stream().flatMap(album -> album.getTracks()).mapToInt(track -> track.getLength()).summaryStatistics();
        System.out.println("max"+intSummaryStatistics.getMax());
        System.out.println("average"+intSummaryStatistics.getAverage());
        System.out.println("count"+intSummaryStatistics.getCount());
        System.out.println("min"+intSummaryStatistics.getMin());
        System.out.println("sum"+intSummaryStatistics.getSum());

        //获取 对应的单个方法
        int length=albums.stream().flatMap(album -> album.getTracks()).mapToInt(track -> track.getLength()).sum();

    }


}
